/**
 * @author casazaft
 */
Package("j2j.css").Class("Css",{
	Css:function(){
		        
        throw Error("can not instance!");
    },
    Statics: function(){
        var ruleCache = new j2j.utils.HashMap();
        var d = document;
        var createStyleSheet = function(cssText, id){
            var ss;
            var head = d.getElementsByTagName("head")[0];
            var styledom = d.createElement("style");
            styledom.setAttribute("type", "text/css");
            id = id || unique();
            styledom.setAttribute("id", id);
            if (System.env.isIE) {
                head.appendChild(styledom);
                ss = getStyleSheet(styledom);
                ss.cssText = cssText;
            }
            else {
                try {
                    styledom.appendChild(d.createTextNode(cssText));
                } 
                catch (e) {
                    styledom.cssText = cssText;
                }
                head.appendChild(styledom);
                ss = getStyleSheet(styledom);
            }
            cacheRules(ss);
            return ss;
        };
        var removeStyleSheet = function(id){
            var ss = d.getElementById(id);
            if (ss) {
                ss.parentNode.removeChild(ss);
                return true;
            }
            return false;
        }
        var cacheRules = function(ss){
            var cssRules = ss.rules || ss.cssRules;
            for (var i = 0; i < cssRules.length; i++) {
                ruleCache.store(cssRules[i].selectorText, cssRules[i]);
            }
        };
        var getStyleSheet = function(styledom){
            return styledom.sheet || styledom.styleSheet || d.styleSheets[d.styleSheets.length - 1];
        };
        var getRule = function(selector){
            return ruleCache.retrieval(selector);
        };
        var eachRules = function(fn){
            ruleCache.each(fn);
        }
        var l2uRe = /(-[a-z])/gi;
        var l2uFn = function($, $1){
            return $1.charAt(1).toUpperCase();
        };
        var updateRule = function(selector, property, value){
            var rule = ruleCache.retrieval(selector);
            var property = property.replace(l2uRe, l2uFn);
            rule.style[property] = value;
        };
        var clearCache = function(){
            cacheRules.clear();
        }
        var changeSkin = function(url, id){
            var head = d.getElementsByTagName("head")[0];
            if (id === undefined) 
                id = unique();
            else {
                var style = d.getElementById(id);
                head.removeChild(style);
            }
            var link = d.createElement("link");
            link.setAttribute("href", url);
            link.setAttribute("rel", "stylesheet");
            link.setAttribute("media", "screen");
            link.setAttribute("type", "text/css");
            link.setAttribute("id", id);
            head.appendChild(link);
            return id;
        };
        return {
            createStyleSheet: createStyleSheet,
            getStyleSheet: getStyleSheet,
            removeStyleSheet: removeStyleSheet,
            cacheRules: cacheRules,
            eachRules: eachRules,
            getRule: getRule,
            updateRule: updateRule,
            changeSkin: changeSkin
        };
    }()
});
